// import { LoggerService } from '@nestjs/common';
import {
    ArgumentsHost,
    Catch,
    ExceptionFilter,
    HttpException,
    HttpStatus,
} from '@nestjs/common';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
    // constructor(private logger: LoggerService) {}
    async catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp(); // 获取请求上下文
        const request = ctx.getRequest(); // 获取请求上下文中的request对象
        const response = ctx.getResponse(); // 获取请求上下文中的response对
        const status = exception.getStatus()
        const exceptionResponse: any = exception.getResponse()

        // 设置错误信息
        const message = exception.message
            ? exception.message
            : `${status >= 500 ? '服务器错误（Service Error）' : '客户端错误（Client Error）'}`;

        // this.logger.error(exception.message, exception.stack);
        // console.log("exception", exception)
        // console.log("exceptionResponse", exceptionResponse)
        console.log("走了http-exception.filter。ts")
        response
            .status(status)
            .json({
                code: status,
                msg: exceptionResponse?.message || message,
                error: exceptionResponse.error,
            });
        // throw new Error('Method not implemented.');
    }
}
